﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace GPSTracker.Helper
{
    public class ExceptionHandler
    {
        public static string GetErrorMsg(Exception ex)
        {
            var s1 = string.Empty;

            try
            {
                if (ex.GetType().FullName == "System.Data.SqlClient.SqlException")
                {
                    var sqlEx = (System.Data.SqlClient.SqlException)ex;

                    if (sqlEx.Number == 2601)
                    {
                        s1 = "Same Record is Already Exist.";
                    }
                    else
                    {
                        if (sqlEx.Number == 547)
                        {
                            s1 = "Record has Reference.";
                        }

                        else
                        {
                            if (sqlEx.Number == 4060)
                            {
                                s1 = "Invalid DataBase.";
                            }
                            else
                            {
                                if (sqlEx.Number == 18452)
                                {
                                    s1 = "Database Login Failed.";
                                }
                                else
                                {
                                    if (sqlEx.Number == 18456)
                                    {
                                        s1 = "Database Login Failed.";
                                    }
                                    else
                                    {
                                        if (sqlEx.Number == 53)
                                        {
                                            s1 = "Wrong Database Server Name .";
                                        }
                                        else
                                        {
                                            if (sqlEx.Number == 512)
                                            {
                                                s1 = "Subquery returned more than 1 value.";
                                            }
                                            else
                                            {
                                                if (sqlEx.Number == 2812)
                                                {
                                                    s1 = "StoreProcedure is not Found.";
                                                }
                                                else
                                                {
                                                    if (sqlEx.Number == -2)
                                                    {
                                                        s1 = "Server Time Out Occured.";
                                                    }
                                                    else
                                                    {
                                                        if (sqlEx.Number == -3)
                                                        {
                                                            s1 = "Sql Zero Bytes.";
                                                        }
                                                        else
                                                        {
                                                            if (sqlEx.Number == -1)
                                                            {
                                                                s1 = sqlEx.Message;
                                                            }
                                                            else
                                                            {
                                                                if (sqlEx.Number == 1)
                                                                {
                                                                    s1 = "Sql Insufficient Memory.";
                                                                }
                                                                else
                                                                {
                                                                    if (sqlEx.Number == 2)
                                                                    {
                                                                        s1 = "Sql Access Denied.";
                                                                    }
                                                                    else
                                                                    {
                                                                        if (sqlEx.Number == 3)
                                                                        {
                                                                            s1 = "Connection is Busy.";
                                                                        }
                                                                        else
                                                                        {
                                                                            if (sqlEx.Number == 4)
                                                                            {
                                                                                s1 = "Connection is Broken.";
                                                                            }
                                                                            else
                                                                            {
                                                                                if (sqlEx.Number == 5)
                                                                                {
                                                                                    s1 = "Connection is Limited.";
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (sqlEx.Number == 6)
                                                                                    {
                                                                                        s1 = "Server is  Not Found.";
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                        if (sqlEx.Number == 7)
                                                                                        {
                                                                                            s1 = "Network is  Not Found.";
                                                                                        }
                                                                                        else
                                                                                        {
                                                                                            if (sqlEx.Number == 8)
                                                                                            {
                                                                                                s1 = "Insufficient Resources.";
                                                                                            }
                                                                                            else
                                                                                            {
                                                                                                if (sqlEx.Number == 9)
                                                                                                {
                                                                                                    s1 = "Network is Busy.";
                                                                                                }
                                                                                                else
                                                                                                {
                                                                                                    if (sqlEx.Number == 10)
                                                                                                    {
                                                                                                        s1 = "Network Access Denied.";
                                                                                                    }
                                                                                                    else
                                                                                                    {
                                                                                                        if (sqlEx.Number == 11)
                                                                                                        {
                                                                                                            s1 = "Sql General Error.";
                                                                                                        }
                                                                                                        else
                                                                                                        {
                                                                                                            if (sqlEx.Number == 12)
                                                                                                            {
                                                                                                                s1 = "Sql Incorrect Mode.";
                                                                                                            }
                                                                                                            else
                                                                                                            {
                                                                                                                if (sqlEx.Number == 13)
                                                                                                                {
                                                                                                                    s1 = "Name Not Found.";
                                                                                                                }
                                                                                                                else
                                                                                                                {
                                                                                                                    if (sqlEx.Number == 14)
                                                                                                                    {
                                                                                                                        s1 = "Invalid Connection.";
                                                                                                                    }
                                                                                                                    else
                                                                                                                    {
                                                                                                                        if (sqlEx.Number == 15)
                                                                                                                        {
                                                                                                                            s1 = "Read Write Error.";
                                                                                                                        }
                                                                                                                        else
                                                                                                                        {
                                                                                                                            if (sqlEx.Number == 16)
                                                                                                                            {
                                                                                                                                s1 = "Too Many Handles.";
                                                                                                                            }
                                                                                                                            else
                                                                                                                            {
                                                                                                                                if (sqlEx.Number == 17)
                                                                                                                                {
                                                                                                                                    s1 = "Server Error.";
                                                                                                                                }
                                                                                                                                else
                                                                                                                                {
                                                                                                                                    if (sqlEx.Number == 18)
                                                                                                                                    {
                                                                                                                                        s1 = "SSL Error.";
                                                                                                                                    }
                                                                                                                                    else
                                                                                                                                    {
                                                                                                                                        if (sqlEx.Number == 19)
                                                                                                                                        {
                                                                                                                                            s1 = "Encryption Error.";
                                                                                                                                        }
                                                                                                                                        else
                                                                                                                                        {
                                                                                                                                            if (sqlEx.Number == 20)
                                                                                                                                            {
                                                                                                                                                s1 = "Encryption is Not Supported.";
                                                                                                                                            }
                                                                                                                                            else
                                                                                                                                            {
                                                                                                                                                if (sqlEx.Number == 2627)
                                                                                                                                                {
                                                                                                                                                    s1 = "UniqueKey";
                                                                                                                                                }
                                                                                                                                                else
                                                                                                                                                {
                                                                                                                                                    s1 = sqlEx.Message;
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (ex.GetType().FullName == "System.DivideByZeroException")
                    {
                        s1 = "Invalid Division Calculation.";
                    }
                    else
                    {
                        if (ex.GetType().FullName == "System.InvalidOperationException")
                        {
                            s1 = "A Method was called at an Invalid Time.";
                        }
                        else
                        {
                            if (ex.GetType().FullName == "System.ArithmeticException")
                            {
                                s1 = "Arithmetic Calculation is not Valid.";
                            }
                            else
                            {
                                if (ex.GetType().FullName == "System.FormatException")
                                {
                                    s1 = "Input Data Format is not Valid.";
                                }
                                else
                                {
                                    if (ex.GetType().FullName == "System.NullReferenceException")
                                    {
                                        s1 = "Object Reference is not set.";
                                    }
                                    else
                                    {
                                        if (ex.GetType().FullName == "System.IndexOutOfRangeException")
                                        {
                                            s1 = "Index is Not Proper.";
                                        }
                                        else
                                        {
                                            if (ex.GetType().FullName == "System.IO.FileNotFoundException")
                                            {
                                                s1 = "File Not Found.";
                                            }
                                            else
                                            {
                                                if (ex.GetType().FullName == "System.InvalidCastException")
                                                {
                                                    s1 = "Invalid Cast.";
                                                }
                                                else
                                                {
                                                    if (ex.GetType().FullName == "System.NotSupportedException")
                                                    {
                                                        s1 = "Non Supported Exception.";
                                                    }
                                                    else
                                                    {
                                                        if (ex.GetType().FullName == "System.ArgumentNullException")
                                                        {
                                                            s1 = "Argument is Null.";
                                                        }
                                                        else
                                                        {
                                                            if (ex.GetType().FullName == "System.SecurityException")
                                                            {
                                                                s1 = "Security Exception.";
                                                            }
                                                            else
                                                            {
                                                                if (ex.GetType().FullName == "System.PathTooLongException")
                                                                {
                                                                    s1 = "Path is Too Long.";
                                                                }
                                                                else
                                                                {
                                                                    if (ex.GetType().FullName == "System.DirectoryNotFoundException")
                                                                    {
                                                                        s1 = "Directory Not Found.";
                                                                    }
                                                                    else
                                                                    {
                                                                        if (ex.GetType().FullName == "System.InvalidProgramException")
                                                                        {
                                                                            s1 = "Invalid Program.";
                                                                        }
                                                                        else
                                                                        {
                                                                            if (ex.GetType().FullName == "System.OverFlowException")
                                                                            {
                                                                                s1 = "Over Flow Exception.";
                                                                            }
                                                                            else
                                                                            {
                                                                                if (ex.GetType().FullName == "System.ArrayTypeMismatchException")
                                                                                {
                                                                                    s1 = "Array Type is Mismatch Exception.";
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (ex.GetType().FullName == "System.ArgumentOutOfRangeException")
                                                                                    {
                                                                                        s1 = "Index was Out of Range.";
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                        s1 = ex.Message.ToString();
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                return "Cannot Connect To The Server !!";
            }

            return s1;
        }
    }
}
